package com.changwu.cwbase.ui.activity.main;

import android.Manifest;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.aspsine.fragmentnavigator.FragmentNavigator;
import com.blankj.utilcode.util.ToastUtils;
import com.changwu.cwbase.App;
import com.changwu.cwbase.R;
import com.changwu.cwbase.base.ActivityBase;
import com.changwu.cwbase.ui.adapter.MainFragmentAdapter;
import com.changwu.cwbase.widget.BottomNavigatorView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends ActivityBase implements BottomNavigatorView.OnBottomNavigatorViewItemClickListener, EasyPermissions.PermissionCallbacks{
    @BindView(R.id.bottomNavigatorView)
    BottomNavigatorView bottomNavigatorView;
    //连续按两次返回键就退出
    private int keyBackClickCount = 0;
    //默认的tab位置,默认为个人中心
    private static final int DEFAULT_POSITION = 0;
    private FragmentNavigator mNavigator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (bottomNavigatorView != null) {
            bottomNavigatorView.setOnBottomNavigatorViewItemClickListener(this);
        }
        mNavigator = new FragmentNavigator(getSupportFragmentManager(), new MainFragmentAdapter(), R.id.container);
        mNavigator.setDefaultPosition(DEFAULT_POSITION);
        mNavigator.onCreate(savedInstanceState);
        setCurrentTab(mNavigator.getCurrentPosition());
        requestCodePermissions();
        initView();
    }


    private void initView() {
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            try {
                // 按两次返回键退出
                switch (keyBackClickCount++) {
                    case 0:
                        ToastUtils.showShort(MainActivity.this.getResources().getString(R.string.press_again_exit));
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                keyBackClickCount = 0;
                            }
                        }, 2000);
                        break;
                    case 1:
                        // 退出
                        App.getInstance().exit();
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBottomNavigatorViewItemClick(int position, View view) {
        setCurrentTab(position);
    }

    private void setCurrentTab(int position) {
        mNavigator.showFragment(position);
        bottomNavigatorView.select(position);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }

    private static final int REQUEST_PERMISSIONS = 1;

    @AfterPermissionGranted(REQUEST_PERMISSIONS)
    private void requestCodePermissions() {
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION
                , Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.RECORD_AUDIO};
        if (!EasyPermissions.hasPermissions(activity, perms)) {
            EasyPermissions.requestPermissions(this, "请打开权限使应用能正常运行", REQUEST_PERMISSIONS, perms);
        } else {
            // Already have permission, do the thing
            App.getInstance().isHasReadPermission = true;
        }
    }
}